package com.site.web.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import com.site.domain.TypicalCases;
import com.site.domain.UserInfo;
import com.site.service.TypicalCasesService;

/**
 * Purpose: 案例
 * @author: liangjun
 * @since: JDK 1.6
 * @date: 2015年9月22日
 */
@Controller
@RequestMapping("/typical/")
public class TypicalController {
	private static Logger logger = LoggerFactory.getLogger(TypicalController.class);
	private static String SYS_LOGIN = "redirect:/sys/login";
	private static String FIND_TYPICAL = "redirect:/typical/findTypical";
	
	@Autowired
	private TypicalCasesService typicalCasesService;
	
	/**
	 * Purpose: 案例查询
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @return: ModelAndView
	 */
	@RequestMapping(value="findTypical", method=RequestMethod.GET)
	public ModelAndView findTypical(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		List<TypicalCases> typicalCasesList = null;
		try {
			typicalCasesList = typicalCasesService.findTypicalCases();
			if(null != typicalCasesList){
				mav.addObject("typicalCasesList", typicalCasesList);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("案例查询异常:"+e.getMessage()+"");
		}
		return mav;
	}
	
	@RequestMapping(value="addTypical", method=RequestMethod.GET)
	public ModelAndView addTypical(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		return mav;
	}
	
	/**
	 * Purpose: 
	 * @author: liangjun
	 * @param request
	 * @param response
	 * @param typicalCases
	 * @return: ModelAndView
	 */
	@RequestMapping(value="addTypical", method=RequestMethod.POST)
	public ModelAndView addTypical(HttpServletRequest request,HttpServletResponse response,@ModelAttribute TypicalCases typicalCases) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			typicalCasesService.addTypicalCases(typicalCases);
			resultCode = 1;
			resultMsg = "添加成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("添加异常:"+e.getMessage()+"");
			resultMsg = "添加异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
	@RequestMapping(value="updTypical", method=RequestMethod.GET)
	public ModelAndView updTypical(HttpServletRequest request,HttpServletResponse response,String id) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		TypicalCases typicalCases = null;
		try {
			typicalCases = typicalCasesService.getTypicalCasesById(Integer.parseInt(id));
			if(null != typicalCases){
				resultCode = 1;
				mav.addObject("typicalCases", typicalCases);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("参数ID类型转换异常:"+e.getMessage()+"");
			resultMsg = "获取失败，无编号ID";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("查询异常:"+e.getMessage()+"");
			resultMsg = "获取失败，查询异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
	@RequestMapping(value="updTypical", method=RequestMethod.POST)
	public ModelAndView updTypical(HttpServletRequest request,HttpServletResponse response,@ModelAttribute TypicalCases typicalCases) {
		ModelAndView mav = new ModelAndView();
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			typicalCasesService.updTypicalCases(typicalCases);
			resultCode = 1;
			resultMsg = "修改成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("修改异常:"+e.getMessage()+"");
			resultMsg = "修改异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	

	@RequestMapping(value="delTypicalById", method=RequestMethod.GET)
	public ModelAndView delTypicalById(HttpServletRequest request,HttpServletResponse response,String id) {
		ModelAndView mav = new ModelAndView(FIND_TYPICAL);
		UserInfo userInfo = null;
		userInfo = (UserInfo) WebUtils.getSessionAttribute(request, "userInfo");
		if(null == userInfo){
			WebUtils.setSessionAttribute(request, "userInfo", null);
			return new ModelAndView(SYS_LOGIN);
		}
		int resultCode = 0;
		String resultMsg = "";
		try {
			typicalCasesService.delTypicalCasesById(Integer.parseInt(id));
			resultCode = 1;
			resultMsg = "删除成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("删除异常:"+e.getMessage()+"");
			resultMsg = "删除异常";
		}
		mav.addObject("resultCode", resultCode);
		mav.addObject("resultMsg", resultMsg);
		return mav;
	}
	
}
